package com.wang.nio;

import java.nio.ByteBuffer;

import org.junit.Test;

public class ByteBufferTest {

	
	@Test
	public void test(){
		ByteBuffer buffer = ByteBuffer.allocate(64);
		Util.displayBufferInfo(buffer, "init");
		buffer.put((byte)'a');
		buffer.put((byte)'b');
		buffer.put((byte)'c');
		Util.displayBufferInfo(buffer, "after put");
		
		//Flips this buffer.(将数据封存,为读取数据做好准备)
		buffer.flip();
		Util.displayBufferInfo(buffer, "after flip");		
		//return the byte at the buffer's current position 
		System.out.println((char)buffer.get());
		Util.displayBufferInfo(buffer,"after a get");
		
		//Clears this buffer.(并不是真的清空, 而是为下一次插入数据做好准备
		buffer.clear();
		Util.displayBufferInfo(buffer, "after clear");
	    // 依然可以访问到数据
	    System.out.println((char) buffer.get(2));

	}
	

}
